package org.csu.mypetstore.persistence.Impl;

import org.csu.mypetstore.domain.Cart;
import org.csu.mypetstore.domain.CartItem;
import org.csu.mypetstore.domain.Item;
import org.csu.mypetstore.domain.LineItem;
import org.csu.mypetstore.persistence.CartDAO;
import org.csu.mypetstore.persistence.DBUtil;
import org.csu.mypetstore.persistence.ItemDAO;

import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class CartDAOImpl implements CartDAO {

    ItemDAO itemDAO = new ItemDAOImpl();

    //sql语句
    private static final String GET_CART_BY_USERNAME = "SELECT ITEMID, QUANTITY FROM CART WHERE USERID = ?";
    private static final String INSERT_CART = "INSERT INTO CART (USERID, ITEMID, QUANTITY) VALUES (?, ?, ?)";
    private static final String DELETE_CART = "DELETE FROM CART WHERE USERID = ? ";
    private static final String UPDATE_CART_ITEM = "UPDATE CART SET QUANTITY = ? WHERE USERID = ? AND ITEMID = ? ";
    private static final String REMOVE_CART = "DELETE FROM CART WHERE USERID = ? AND ITEMID = ?";
    //根据用户名找cart
    @Override
    public Cart getCartByUsername(String username) {
        Cart cart = new Cart();
        int flag = 0;
        try{
            Connection connection = DBUtil.getConnection();
            PreparedStatement preparedStatement = connection.prepareStatement(GET_CART_BY_USERNAME);
            preparedStatement.setString(1,username);
            ResultSet resultSet = preparedStatement.executeQuery();

            while(resultSet.next()){
                flag = 1;
                CartItem cartItem = new CartItem();
                cartItem.setItem(itemDAO.getItem(resultSet.getString(1)));
                cartItem.setQuantity(resultSet.getInt(2));
                cart.addItem(cartItem);
            }
            DBUtil.closeResultSet(resultSet);
            DBUtil.closePreparedStatement(preparedStatement);
            DBUtil.closeConnection(connection);

        }catch (Exception e){
            e.printStackTrace();
        }
        if(flag == 0){
            return null;
        }else {
            System.out.println("找到cart啦" + username);
            return cart;
        }
    }

    //插入Cart
    @Override
    public boolean insertCart(CartItem cartItem,String  username) {
        boolean flag = false;

        try{
            Connection connection = DBUtil.getConnection();
            PreparedStatement preparedStatement = connection.prepareStatement(INSERT_CART);
            preparedStatement.setString(1,username);
            preparedStatement.setString(2,cartItem.getItem().getItemId());
            preparedStatement.setInt(3,cartItem.getQuantity());

            int row = preparedStatement.executeUpdate();
            if(row == 1){
                flag = true;
                System.out.println("插入一项到cart" + cartItem.getItem().getItemId()+cartItem.getQuantity());
            }
            DBUtil.closePreparedStatement(preparedStatement);
            DBUtil.closeConnection(connection);
        }catch (Exception e){
            e.printStackTrace();
        }
        return flag;
    }

    //删除Cart
    @Override
    public boolean deleteCartByUsername(String username){
        boolean flag = false;

        try{
            Connection connection = DBUtil.getConnection();
            PreparedStatement preparedStatement = connection.prepareStatement(DELETE_CART);
            preparedStatement.setString(1,username);

            int row = preparedStatement.executeUpdate();
            if(row >= 1){
                flag = true;

                System.out.println("删除"+username+"cart"  );
            }
            DBUtil.closePreparedStatement(preparedStatement);
            DBUtil.closeConnection(connection);
        }catch (Exception e){
            e.printStackTrace();
        }
        return flag;
    }


    //更新Cart
    @Override
    public boolean updateCartItem(String username ,String itemId,int quantity){
        boolean flag = false;

        try{
            Connection connection = DBUtil.getConnection();
            PreparedStatement preparedStatement = connection.prepareStatement(UPDATE_CART_ITEM);
            preparedStatement.setInt(1,quantity);
            preparedStatement.setString(2,username);
            preparedStatement.setString(3,itemId);

            int row = preparedStatement.executeUpdate();
            if(row == 1){
                System.out.println("更新指定的cart" + username+itemId+quantity);
                flag = true;
            }
            DBUtil.closePreparedStatement(preparedStatement);
            DBUtil.closeConnection(connection);
        }catch (Exception e){
            e.printStackTrace();
        }
        return flag;
    }


    //remove Cart
    @Override
    public boolean removeCartItem(String username ,String itemId){
        boolean flag = false;

        try{
            Connection connection = DBUtil.getConnection();
            PreparedStatement preparedStatement = connection.prepareStatement(REMOVE_CART);
            preparedStatement.setString(1,username);
            preparedStatement.setString(2,itemId);

            int row = preparedStatement.executeUpdate();
            if(row == 1){

                System.out.println("删除指定的cart" + username+itemId);
                flag = true;
            }
            DBUtil.closePreparedStatement(preparedStatement);
            DBUtil.closeConnection(connection);
        }catch (Exception e){
            e.printStackTrace();
        }
        return flag;

    }
}
